/**
 * Created by zhanglihe on 16/1/23.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog'
], function( $, common, ready, Collection, template, Model, dialog ) {

    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'user/master/user_tags',
                model: {
                    url: {
                        delete: _global.url.api + 'user/master/user_tag/{id}'
                    },
                    event: {
                        '.item-remove': 'remove'
                    },
                    remove: function(e) {
                        e.preventDefault();
                        var _this = this;
                        dialog({
                            content: '删除后无法恢复，确定要删除吗?',
                            ok: function() {
                                _this.del();
                            }, cancel: function() {}
                        }).show(e.target);
                    }
                },
                callback: function() {
                    loaded();//移除加载效果
                }
            });
            this.collection.fetch();
        },
        event: function() {
            var _this = this;
            $('#btn-add').on('click', function(e) {
                e.preventDefault();
                dialog({
                    content: template('add-tpl', {}),
                    ok: function() {
                        var data = {
                            name: $('[name="name"]').val(),
                            intro: $('[name="intro"]').val(),
                            listorder: $('[name="listorder"]').val()
                        };
                        new Model({
                            url: _global.url.api + 'user/master/user_tag',
                            data: data,
                            success: function() {
                                _this.collection.reload();
                            }
                        }).post();
                    }, cancel: function() {}
                }).show();
            });
        }
    };
    new index();
});